package org.example.controller;

import jakarta.annotation.Resource;
import org.example.entity.*;
import org.example.feign.CartServerService;
import org.example.service.OrdersDetailetService;
import org.example.service.OrdersService;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RefreshScope
public class OrdersController {
    @Resource
    private OrdersService ordersService;
    @Resource
    private OrdersDetailetService ordersDetailetService;
    @Resource
    private CartServerService cartServerService;

    @GetMapping("/OrdersController/addOrder/{userId}/{businessId}/{daId}/{orderTotal}")
    public CommonResult<Orders> addOrder(
            @PathVariable("userId") String userId,
            @PathVariable("businessId") Integer businessId,
            @PathVariable("daId") Integer daId,
            @PathVariable("orderTotal") Double orderTotal) throws Exception{
        Orders order = new Orders();
        order.setUserId(userId);
        order.setBusinessId(String.valueOf(businessId));
        order.setDaId(daId);
        order.setOrderTotal(orderTotal);
        order.setOrderState(0);//默认是0, 表示未支付
        order.setOrderDate(new Date()); //时间自动创建, 在Orders类中弄不了, 只好在这里弄了, 注意这里接受了一个Date类, 符合这个字段的数据类型
        ordersService.save(order);

        //根据购物车上的订单 创建 Orderdetailet
        List<Orderdetailet> orderDetailList = new ArrayList<>();
        List<Cart> cartList = cartServerService.findAllByUidOrByUidAndBid(userId, String.valueOf(businessId)).getResult();
        for (Cart c : cartList){
            Orderdetailet od = new Orderdetailet();
            od.setFoodId(c.getFoodId());
            od.setQuantity(c.getQuantity());
            od.setOrders(order);//
//            ordersDetailetService.save(od);
            orderDetailList.add(ordersDetailetService.save(od));
        }
        order.setList(orderDetailList); //设置list对象
        //对当前的order对象再做一次save操作，此时相当于update（更新order对象的list字段。
        ordersService.save(order);//再做一次 save
        //添加了一个订单, 那么根据业务上, 需要在用户的购物车上删除相应的记录:)
        cartServerService.deleteByUidAndBidAndFidOrByUidAndBid(order.getUserId(), order.getBusinessId(),null);
        return new CommonResult<>(200,"订单创建成功",order);
    }


    @GetMapping("/OrdersController/getOrdersById/{orderId}")
    public CommonResult<Orders> getOrdersById(@PathVariable("orderId") Integer orderId){
        Orders orders = ordersService.findByOrderId(orderId);
        if(orders!=null){
            return new CommonResult<>(200,"查找订单成功",orders);
        }
        return new CommonResult<>(404,"查找订单失败，没有该orderId",null);
    }

    @GetMapping("/OrdersController/listOrdersByUserId/{userId}")
    public CommonResult<List<Orders>> findAllByUserId(@PathVariable("userId") String userId){
        List<Orders> ordersList = ordersService.findAllByUserId(userId);
        return new CommonResult<>(200,"findAllByUserId成功", ordersList);
    }
}
